package com.feri.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feri.shop.dao.*;
import com.feri.shop.dto.OrderCartDto;
import com.feri.shop.dto.OrderGoodsDto;
import com.feri.shop.entity.Order;
import com.feri.shop.entity.OrderItem;
import com.feri.shop.entity.OrderLog;
import com.feri.shop.service.intf.OrderService;
import com.feri.shop.util.SnowFlakeUtils;
import com.feri.shop.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao dao;
    @Autowired
    private OrderItemDao itemDao;
    @Autowired
    private OrderLogDao logDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private CartItemDao cartItemDao;

    //购物车-选择-订单确认-下单
    @Override
    @Transactional //开启事务
    public R save(OrderCartDto dto) {
        //1.验证
        if(dto!=null && dto.getAid()>0 && dto.getDtoList()!=null){
            //2.验证库存 略
            //3.创建订单对象
            Order order=new Order(
                    SnowFlakeUtils.getInstance().nextId()+"",
                    dto.getTmoney(),dto.getFmoney(),
                    dto.getTmoney()-dto.getFmoney()
            ,dto.getAid(),dto.getUid(),dto.getInfo());
            //4.新增数据
            dao.insert(order);
            //5.新增订单详情--可以Mybatis的批处理
            dto.getDtoList().forEach((o)->{
                OrderItem item=new OrderItem(o.getPrice(),o.getNum(),
                        order.getId(),o.getGid());
                itemDao.insert(item);
                //6.扣减库存
                goodsDao.updateStock(o.getGid(),o.getNum());
            });
            //7.回清购物车
            cartItemDao.delBatch(dto.getCiids());
            //8.记录流水
            logDao.insert(new OrderLog(order.getId(),1,"选择购物车，创建了订单"));
            //9.返回结果
            return R.ok(order);
        }
        return R.fail("请检查参数信息");
    }
    //商品详情-立即购买-订单确认-下单
    @Override
    public R save(OrderGoodsDto dto) {
        //1.验证
        if(dto!=null && dto.getAid()>0 && dto.getItem()!=null){
            //2.验证库存 略
            //3.创建订单对象
            Order order=new Order(
                    SnowFlakeUtils.getInstance().nextId()+"",
                    dto.getTmoney(),dto.getFmoney(),
                    dto.getTmoney()-dto.getFmoney()
                    ,dto.getAid(),dto.getUid(),dto.getInfo());
            //4.新增数据
            dao.insert(order);
            //5.新增订单详情--可以Mybatis的批处理
            OrderItem item=new OrderItem(dto.getItem().getPrice(),dto.getItem().getNum(),
                    order.getId(),dto.getItem().getGid());
            itemDao.insert(item);

            //6.扣减库存
            goodsDao.updateStock(dto.getItem().getGid(),dto.getItem().getNum());
            //7.记录流水
            logDao.insert(new OrderLog(order.getId(),1,"商品详情，立即购买，创建了订单"));
            //8.返回结果
            return R.ok(order);
        }
        return R.fail("请检查参数信息");
    }

    @Override
    public R queryMy(long uid,int type) {
        //1.校验
        if(uid>0){
            //2.构建查询条件对象
            QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("uid",uid);
            //3.验证查询的订单状态
            if(type!=5){
                if(type==4){
                    type=5;
                }
               queryWrapper.eq("flag",type);
            }
            queryWrapper.orderByDesc("ctime");

            //3.返回结果
            return R.ok(dao.selectList(queryWrapper));
        }
        return R.fail("请检查参数信息！");
    }

    //1.订单信息 2.订单详情（1.商品信息） 3.评价信息 4.订单状态变化 5.收货地址 6.支付信息 7.物流信息
    @Override
    public R queryDetail(long oid) {
        if(oid>0){
            return R.ok(dao.selectMy(oid));
        }
        return R.fail("请检查参数信息！");
    }

    @Override
    public R change(long oid, int flag) {
        //1.验证
        if(oid>0 && flag>0){
            //2.更改状态
            dao.updateFlag(oid, flag);
            //3.记录流水
            logDao.insert(new OrderLog(oid,flag,"更改了订单状态！"));
            //返回
            return R.ok();
        }
        return R.fail("请检查参数信息");
    }

    @Override
    public R cancel(long oid) {
        //1.验证
        if(oid>0){
            //2.修改订单状态
            if(dao.updateFlag(oid,8)>0){
                //3.查询订单详情
                QueryWrapper<OrderItem> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("oid",oid);
                List<OrderItem> list=itemDao.selectList(queryWrapper);
                //4.释放库存,再把商品库存加回去
                list.forEach(o->{
                    goodsDao.updateStock(o.getGid(),-o.getNum());
                });
                //5.记录日志
                logDao.insert(new OrderLog(oid,8,"取消了订单，释放资源！"));
                //6.返回
                return R.ok();
            }
        }
        return R.fail("请检查参数信息");
    }
}
